package de.ret_world.openrpgstudio.mapeditor;

import buoy.event.CommandEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import de.ret_world.openrpgstudio.mapeditor.scripts.TileMap;
import java.awt.Insets;

/**
 * This Dialog is called by MapEditor for creating and editing TileMaps.
 *
 * @author Emu
 */
public class EditMapDialog {

    private BFrame parent;
    private BDialog dialog;
    private TileMap tilemap;
    private BTextField txtID;
    private BSpinner spnWidth;
    private BSpinner spnHeight;
    private BSpinner spnTWidth;
    private BSpinner spnTHeight;
    private boolean ok = false;

    public EditMapDialog(BFrame parent) {
        this.parent = parent;
    }

    public boolean showDialog(TileMap tilemap) {
        this.tilemap = tilemap;
        Internationalization intn = Internationalization.getInstance();
        LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);

        dialog = new BDialog(parent, intn.getString("ME.EDITMAP"), true);

        ColumnContainer cc = new ColumnContainer();
        cc.add(new BLabel(intn.getString("ME.MAPID")), li);
        txtID = new BTextField(20);
        txtID.setText(tilemap.getID());
        cc.add(txtID, li);
        cc.add(new BLabel(intn.getString("ME.MAPSIZE")), li);
        RowContainer rc = new RowContainer();
        spnWidth = new BSpinner(25, 3, 1024, 1);
        spnWidth.setValue(tilemap.getWidth());
        rc.add(spnWidth);
        rc.add(new BLabel("x"));
        spnHeight = new BSpinner(25, 3, 1024, 1);
        spnHeight.setValue(tilemap.getHeight());
        rc.add(spnHeight);
        cc.add(rc, li);
        cc.add(new BLabel(intn.getString("ME.TILESIZE")), li);
        rc = new RowContainer();
        spnTWidth = new BSpinner(25, 3, 1024, 1);
        spnTWidth.setValue(tilemap.getTileWidth());
        rc.add(spnTWidth);
        rc.add(new BLabel("x"));
        spnTHeight = new BSpinner(25, 3, 1024, 1);
        spnTHeight.setValue(tilemap.getTileHeight());
        rc.add(spnTHeight);
        cc.add(rc, li);

        BorderContainer bc2 = new BorderContainer();
        BButton btn = new BButton(intn.getString("OK"));
        btn.addEventLink(CommandEvent.class, this, "ok");
        bc2.add(btn, BorderContainer.EAST, li);
        btn = new BButton(intn.getString("CANCEL"));
        btn.addEventLink(CommandEvent.class, this, "cancel");
        bc2.add(btn, BorderContainer.CENTER, li);
        cc.add(bc2, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.NONE, new Insets(5, 5, 5, 5), null));

        dialog.setContent(cc);
        dialog.addEventLink(WindowClosingEvent.class, this, "cancel");
        dialog.setResizable(false);
        dialog.pack();
        dialog.setVisible(true);

        return ok;
    }

    private void cancel() {
        dialog.dispose();
    }

    private void ok() {
        tilemap.setID(txtID.getText());
        tilemap.setHeight((int) spnHeight.getValue());
        tilemap.setWidth((int) spnWidth.getValue());
        tilemap.setTileHeight((int) spnTHeight.getValue());
        tilemap.setTileWidth((int) spnTWidth.getValue());

        ok = true;
        dialog.dispose();
    }
}
